#include "gamelogic.h"
#include "qipan.h"
#include <QDebug>
GameLogic::GameLogic(QObject *parent) : QObject(parent)
{

}

/*判断角色是否获胜*/
bool GameLogic::checkWin(int row, int col, Role role)
{
    /*四个方向*/
    const int directions[4][2] =
    {
        /*水平*/
        {1,0},
        /* 垂直*/
        {0,1},
        /*左对角*/
        {1,1},
        /*右对角*/
        {1,-1}
    };
    /*四个方向探测*/
    for(int direct=0;direct<4;direct++)
    {
        /*初始化计数器*/
        int cnt = 1;
        /*向一个方向的正方向探测*/
        for(int idx=1;idx<5;idx++)
        {
            int newRow = row + directions[direct][0] *idx;
            int newCol = col + directions[direct][1] *idx;
            if((newRow >=0 && newRow <m_boardSize)&&
                    (newCol>=0 &&newCol<m_boardSize)&&
                    m_board[newRow][newCol]== role)
            {
                cnt++;
            }
            else {
                break;
            }
        }
        for(int idx=1;idx<5;idx++)
        {
            int newRow = row - directions[direct][0] *idx;
            int newCol = col - directions[direct][1] *idx;
            if((newRow >=0 && newRow <m_boardSize)&&
                    (newCol>=0 &&newCol<m_boardSize)&&
                    m_board[newRow][newCol]== role)
            {
                cnt++;
            }
            else {
                break;
            }
        }
        if(cnt >= 5)
        {
            return true;
        }

    }
    return  false;
}
/*设置棋盘信息*/
void GameLogic::setQipanBoard(Role **board,int boardSize)
{
    m_board =board;
    m_boardSize = boardSize;
}
/*人机移动*/
void GameLogic::computerMove()
{
    qDebug()<<"我选择哪个位置呢"<<endl;
    int bestScore=0;
    int bestRow=-1;
    int bestCol=-1;
    int boardSize = m_boardSize;
    for(int row=0;row<boardSize;row++)
    {
        for(int col=0;col<boardSize;col++)
        {
            if(m_board[row][col]==0)
            {
                /*判断这个点AI是不是最有利的
                1.对AI最有利的
                2.对对手最不利（[对对手最有利])*/
                int score=evaluateMove(row,col,COMPUTER)+evaluateMove(row,col,PLATER);
                if(bestScore<score)
                {
                    bestScore = score;
                    bestRow = row;
                    bestCol =col;
                }

            }
        }
    }
    qDebug()<<"bestRow:"<<bestRow<<"bestCol:"<<bestCol<<endl;
    emit Aiposition(bestRow,bestCol);
}

int GameLogic::evaluateMove(int row, int col, Role role)
{
    /*连子数+开放端*/
    /*四个方向*/
    //连子数 + 开发端
    // 四个方向
    const int directions[4][2] =
    {
        {1, 0},     // 水平方向
        {0, 1},     // 垂直方向
        {1, 1},     // 左对角线
        {1, -1}     // 右对角线
    };

    //分数
    int score =0;

    // 四个方向探测
    for (int direction = 0; direction < 4; direction++)
    {
        //连子数
        int count=0;

        //开放端
        int openEnds=0;

        //向同一方向检查最多四个棋子
        for(int idx =1;idx <=4;idx++)
        {
            int newRow = row + directions[direction][0] * idx;
            int newCol = col + directions[direction][1] * idx;

            if (newRow >= 0 && newRow < m_boardSize && newCol >= 0 && newCol < m_boardSize)
            {
                if(m_board[newRow][newCol] == role)
                {
                    count++;
                }
                else if(m_board[newRow][newCol]== EMPTY)
                {
                    openEnds++;
                    break;
                }
                else {
                    //遇到对手的棋子
                    break;
                }
            }
            else {
                break;
            }
        }

        for(int idx =1;idx <=4;idx++)
        {
            int newRow = row - directions[direction][0] * idx;
            int newCol = col - directions[direction][1] * idx;

            if (newRow >= 0 && newRow < m_boardSize && newCol >= 0 && newCol < m_boardSize)
            {
                if(m_board[newRow][newCol] == role)
                {
                    count++;
                }
                else if(m_board[newRow][newCol]== EMPTY)
                {
                    openEnds++;
                    break;
                }
                else {
                    //遇到对手的棋子
                    break;
                }
            }
            else {
                break;
            }
        }

        if(count == 4 && openEnds >0)
        {
            score += 10000;
        }
        else if(count == 3&& openEnds >0)
        {
            score += 1000;
        }
        else if(count ==2 && openEnds >0)
        {
            score +=100;
        }
        else if(count == 1 && openEnds >0)
        {
            score +=10;
        }
    }

    return score;

}
